#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
#include "image.h"
#include <iostream>

using namespace std;

SDL_Surface *load_image(std::string filename){
	SDL_Surface* loadedImage = NULL;
	SDL_Surface* optimizedImage = NULL;
	loadedImage = IMG_Load(filename.c_str());
	
	if (loadedImage != NULL){
	  optimizedImage = SDL_DisplayFormat(loadedImage);
	  SDL_FreeSurface(loadedImage);
	}
	else
	  {
	    cout << "Error loading image!" << endl;
	    cout << "Filename : " << filename << endl << flush;
	    return NULL;
	  }
	return optimizedImage;
}

SDL_Surface *load_bmp(string filename){
  SDL_Surface* loadedImage = NULL;
  SDL_Surface* optimized = NULL;
  loadedImage =SDL_LoadBMP(filename.c_str());
  
  if (loadedImage != NULL){
    optimized = SDL_DisplayFormat(loadedImage);
    SDL_FreeSurface(loadedImage);
  }
  else
    {
      cout << "Error loading image\n Filename : " << filename << endl << flush;
      return NULL;
    }
  return optimized;
}

void apply_surface(int x, int y, SDL_Surface* src, SDL_Surface* dest){
  SDL_Rect offset;
  offset.y = y;
  offset.x = x;
  SDL_BlitSurface(src, NULL, dest, &offset);
  if (SDL_Flip(dest)==-1){
    cout << "Error please restart game ! " << endl << flush ;
      }
}

void DrawBlank(int x, int y,SDL_Surface* src){
  SDL_Surface* blank;
  blank = load_image("./img/white.bmp");
  apply_surface(x,y,blank,src);
}
